package com.shine.app.commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.ContentResolver;
import android.content.Context;

public class Calendars {

	private Calendar calendar;

	public static synchronized Calendars getInstance() {
		Calendars instance = null;
		if (instance == null) {
			instance = new Calendars();
		}
		return instance;
	}

	public String date() {
		calendar = Calendar.getInstance();
		String date = (calendar.get(Calendar.MONTH) + 1) + "/"
				+ calendar.get(Calendar.DAY_OF_MONTH) + "/"
				+ calendar.get(Calendar.YEAR);
		date = format(date);
		return date;
	}

	public String formatCourseReviewDate(String input) {
		// 2013-04-11T23:26:45+00:00
		if (!StringUtils.isEmpty(input)) {
			SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
			SimpleDateFormat format1 = new SimpleDateFormat(
					"yyyy-MM-dd'T'HH:mm:ss+00:00");
			Date date = null;
			try {
				date = format1.parse(input.toString().trim());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (date != null) {
				return format.format(date);
			} else {
				return input.substring(0, input.indexOf("T"));
			}
		}
		return "";
	}

	public String formatBookingShareDate(String input) {
		// 2014-10-30T04:55:14.857+00:00
		if (!StringUtils.isEmpty(input)) {
			SimpleDateFormat format = new SimpleDateFormat(
					"MM/dd/yyyy HH:mm:ss");
			SimpleDateFormat format1 = new SimpleDateFormat(
					"yyyy-MM-dd'T'HH:mm:ss+00:00");
			Date date = null;
			try {
				date = format1.parse(input.toString().trim());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (date != null) {
				return format.format(date);
			} else {
				return input.substring(0, input.indexOf("T"));
			}
		}
		return "";
	}

	public String formatTeeTimeDate(String input) {
		// 2014-10-31T09:00:00
		if (!StringUtils.isEmpty(input)) {
			SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
			SimpleDateFormat format1 = new SimpleDateFormat(
					"yyyy-MM-dd'T'HH:mm:ss");
			Date date = null;
			try {
				date = format1.parse(input.toString().trim());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (date != null) {
				return format.format(date);
			} else {
				return input.substring(0, input.indexOf("T"));
			}
		}
		return "";
	}

	public String format(String data) {
		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		Date date = null;
		try {
			date = format.parse(data);
		} catch (ParseException e) {
			e.printStackTrace();
			return data;
		}
		return format.format(date);
	}

	public String format_Line(String data) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
		Date date = null;
		try {
			date = format.parse(data);
		} catch (ParseException e) {
			e.printStackTrace();
			return data;
		}
		return format.format(date);
	}

	public String formatSimple(String data) {
		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
		Date date = null;
		try {
			date = format.parse(data);
		} catch (ParseException e) {
			e.printStackTrace();
			return data;
		}
		return format.format(date);
	}

	public String formatLine(String data) {
		String result = formatSimple(data);
		result = result.replaceAll("/", "-");
		return result;
	}

	public String getTeeTime(String date, Context context) {
		ContentResolver contentResolver = context.getContentResolver();
		String timeFormat = android.provider.Settings.System.getString(
				contentResolver, android.provider.Settings.System.TIME_12_24);
		if (StringUtils.isEmpty(timeFormat)) {
			return format_to12Hours(date);
		} else {
			if (timeFormat.equals("12")) {
				return format_to12Hours(date);
			} else {
				return format_to24Hours(date);
			}
		}
	}

	public String getTeeTimeLowercase(String date, Context context) {
		ContentResolver contentResolver = context.getContentResolver();
		String timeFormat = android.provider.Settings.System.getString(
				contentResolver, android.provider.Settings.System.TIME_12_24);
		if (StringUtils.isEmpty(timeFormat)) {
			return format_to12HoursLowercase(date);
		} else {
			if (timeFormat.equals("12")) {
				return format_to12HoursLowercase(date);
			} else {
				return format_to24Hours(date);
			}
		}
	}

	public String format_to12Hours(String data) {
		String result;
		SimpleDateFormat format = new SimpleDateFormat("hh:mm");
		Date date = null;
		try {
			date = format.parse(data);
			result = format.format(date);
			int hour = date.getHours();
			if (hour > 11 || hour == 0) {
				result = result + "PM";
			} else {
				result = result + "AM";
			}
		} catch (ParseException e) {
			e.printStackTrace();
			result = data;
		}
		return result;
	}

	public String format_to12HoursLowercase(String data) {
		String result;
		SimpleDateFormat format = new SimpleDateFormat("hh:mm");
		Date date = null;
		try {
			date = format.parse(data);
			result = format.format(date);
			int hour = date.getHours();
			if (hour > 11 || hour == 0) {
				result = result + "pm";
			} else {
				result = result + "am";
			}

		} catch (ParseException e) {
			e.printStackTrace();
			result = data;
		}
		return result;
	}

	public String format_to24Hours(String data) {
		String result;
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		Date date = null;
		try {
			date = format.parse(data);
			result = format.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			result = data;
		}
		return result;
	}

	public String getRangebarTime(int time) {
		time = time + 6;
		String result = String.valueOf(time);
		if (time <= 12) {
			result = time + "AM";
		} else {
			time = time - 12;
			result = time + "PM";
		}
		return result;
	}
}
